/**
 * 
 */
package org.t2.backstage.module.mail;

import org.gaming.backstage.PageData;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.service.asserts.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.TipsCode;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.mail.struct.PlayerMailQuery;
import org.t2.backstage.module.player.model.Player;
import org.t2.backstage.module.player.service.PlayerService;
import org.t2.backstage.util.T2HttpUtil;

import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.mail.MailApiProtocol.MailDeletePlayerRequest;
import sh.t2.share.module.mail.MailApiProtocol.MailDeletePlayerResponse;
import sh.t2.share.module.mail.MailApiProtocol.MailQueryPlayerRequest;
import sh.t2.share.module.mail.MailApiProtocol.MailQueryPlayerResponse;
import sh.t2.share.module.mail.MailData;

/**
 * @author YY
 *
 */
@Controller
public class PlayerMailController {

	@Autowired
	private PlayerService playerService;
	@Autowired
	private GameServerService gameServerService;

	@ApiDocument("请求玩家邮件数据")
	@RequestMapping(value = "/playerMail/data.auth")
	@ResponseBody
	public PageData<MailData> data(PlayerMailQuery query) {
		Player player = playerService.getEntity(query.getPlayerId());
		Asserts.isTrue(player != null, TipsCode.PLAYER_NOT_EXIST, query.getPlayerId());

		GameServer gameServer = gameServerService.getByPlatformAndServer(player.getPlatformId(), player.getServerId());

		MailQueryPlayerRequest request = new MailQueryPlayerRequest();
		request.playerId = player.getId();
		
		ResponseResult<MailQueryPlayerResponse> response = T2HttpUtil.jsonPost(gameServer, GameApi.queryPlayerMails, request, MailQueryPlayerResponse.class);
		MailQueryPlayerResponse mailResponse = response.getData();
		
		PageData<MailData> pageData = new PageData<>();
		pageData.setData(mailResponse.datas);
		pageData.setCount(mailResponse.datas.size());
		return pageData;
	}

	@ApiDocument("删除玩家邮件数据")
	@RequestMapping(value = "/playerMail/delete.authw")
	@ResponseBody
	public void deletePlayerMails(@RequestParam("playerId") long playerId, @RequestParam("mailId") long mailId) {
		Player player = playerService.getEntity(playerId);
		Asserts.isTrue(player != null, TipsCode.PLAYER_NOT_EXIST, playerId);

		GameServer gameServer = gameServerService.getByPlatformAndServer(player.getPlatformId(), player.getServerId());

		MailDeletePlayerRequest request = new MailDeletePlayerRequest();
		request.playerId = player.getId();
		request.mailId = mailId;
		ResponseResult<MailDeletePlayerResponse> response = T2HttpUtil.jsonPost(gameServer, GameApi.deletePlayerMail, request, MailDeletePlayerResponse.class);
		Asserts.isTrue(response != null && response.getCode() == 0, TipsCode.MAIL_DELETE_PLAYER_ERROR);
	}
}
